import requests 
import pandas as pd


class ShapeStrategy():
    """ 形态选标的
    """
    @classmethod
    def xg_golden_index(cls):
        """策略金股
        """

        url = "https://bisheng.tenpay.com/fcgi-bin/xg_golden_index.fcgi?user_type=4&guid=EFF919C8-ADA8-48FE-BDB3-6FD8F7224663&_appName=ios&_dev=iPhone12,5&_devId=4f4fa86d37f46aa2e5b759b9dbf719a8b557ec59&_appver=8.8.5&_ifChId=&_isChId=1&_osVer=14.2&openid=oA0GbjhnCVhlaOlVm5vXzQy-28Mo&fskey=v0aaf8a822160499a0b98c1a62919ba5&appid=wxcbc3ab3807acb685&access_token=43_nxkkOvNEbx5qg3wBnjGTrbInYbR3LPqm3ndZcZRXyns3o-rD5ccL_g-5RSNR5uqQKUsqzPIBVacoGDCl4Ix0col6R8fkc_hodKylQsgGs5E&buildType=store&check=11&_idfa=00000000-0000-0000-0000-000000000000&lang=zh_CN"
        h = {
            "user-agent": "QQStock/21022615 CFNetwork/1206 Darwin/20.1.0",
            "content-type": "application/x-www-form-urlencoded",
        }

        data = "ywid=CLJG&cond_index=-1&exchange=12&user_type=4&source=xg&time=1617173960&sign=69bf034908b7e33f4b0d5812790cd29d"
        r = requests.post(url, headers=h,data=data)
        rj = r.json()
        return rj 
        

    @classmethod
    def xg_cp_index(cls):
        """根据形态选择, index
        """

        url = "https://bisheng.tenpay.com/fcgi-bin/xg_cp_index.fcgi?user_type=4&guid=A4B66B6B-5E86-44E7-9AE1-94963CBB29C6&_appName=ios&_dev=iPhone12,5&_devId=4f4fa86d37f46aa2e5b759b9dbf719a8b557ec59&_appver=8.8.5&_ifChId=&_isChId=1&_osVer=14.2&openid=oA0GbjhnCVhlaOlVm5vXzQy-28Mo&fskey=v0aaf8a822160499a0b98c1a62919ba5&appid=wxcbc3ab3807acb685&access_token=43_nxkkOvNEbx5qg3wBnjGTrbInYbR3LPqm3ndZcZRXyns3o-rD5ccL_g-5RSNR5uqQKUsqzPIBVacoGDCl4Ix0col6R8fkc_hodKylQsgGs5E&buildType=store&check=11&_idfa=00000000-0000-0000-0000-000000000000&lang=zh_CN"
        h = {
            "user-agent": "QQStock/21022615 CFNetwork/1206 Darwin/20.1.0",
            "content-type": "application/x-www-form-urlencoded",
        }

        data = "exchange=12&user_type=4&source=xg&time=1617175520&sign=a6253bdadbc57e12602a4449b4507cae"
        r = requests.post(url, headers=h,data=data)
        rj = r.json()

    @classmethod
    def xg_cp_detail(cls, chart_id):
        """根据形态选择
        {
            "10001": [
                "V形底",
                ["形态形成于股票连续下跌期", "转势点要有明显的大成交量配合"],
                "V形底又称尖底，是指股价连续下跌后踩到重要支撑位，以V形反转方式连续长涨反攻，形成V形反转的底部样式。是一种变化较快、转势力度极强的反转形态。"
            ],
            "10002": [
                "圆形底",
                ["下降形态倾斜较为平稳", "中期阳线放量，阴线缩量，股价横盘", "上升形态较为平稳，高点价格突破颈线"],
            	"圆形底形态属于一种盘整形态，多出现在价格底部区域，是极弱势行情的典型特征。其形态表现在K线图中宛如锅底状。"
            ],
            "10003": [
                "双底",
                ["形态形成于股票连续下跌期", "双底的位置相当", "第二次反弹突破第一次反弹的高点，并伴随成交量放大"],
                "双重底也称“W底”，是指股票的价格在连续二次下跌的低点大致相同时形成的股价走势图形。两个跌至最低点的连线叫支撑线。它的形成是这样的，在下跌行情的末期，市场里股票的出售量减少，股价跌到一定程度后，开始不再继续下跌。"
            ],
            "10004": [
                "头肩底",
                ["三个波谷成交量逐步放大", "有效突破颈线阻力后，形态形成"],
                "头肩底以左肩、底、右肩及颈线形成。 三个波谷成交量逐步放大，有效突破颈线阻力后，形态形成。股价反转高度一般都大于颈线与最低点之间的垂直高度。"
            ],
            
            "20013": [
                "红三兵",
                ["第三天收盘价大于第二天收盘价", "第二天收盘价大于第一天收盘价", "这三天的收盘价都大于这三天的开盘价"],
                "红三兵是三根阳线，依次上升，形成红三兵形态。它是一种很常见的 K线组合，这种K线组合出现时，后势看涨的情况居多。"
            ],
            "20014": [
                "早晨之星", 
                ["第一天阴线收盘", "第二天跳空向下，且收盘收为十字星", "第三天跳空向上，阳线收盘且收盘价要高于第二天"] 
                '早晨之星又称“黎明之星”，是由三根K线组成的K线组合形态，它是一种行情见底转势的形态。这种形态如果出现在下降趋势中应引起注意，因为此时趋势已发出比较明确的反转信号。'
            ],
            "20015": [
                "一阳三线",
                ["当日收盘涨幅大于3%", "当日最低价低于5日，10日，20日均线", "当日收盘价大于5日，10日，20日均线"],
                "一阳三线是指5日，10日，20日均线处于聚合状态，当日收盘一个根大阳线，同时上穿5日，10日，20日均线。"
            ],
            "20016": [
                "出水芙蓉",
                ["当日收盘价格大于5日，10日，20日均线", "昨日收盘价小于5日，10日，20日均线最大值", "今日成交量是昨日两倍，以阳线收盘"],
                "出水芙蓉是一种经典底部启动形态。个股在横盘整理或下跌过程当中，某天突然一根长阳线突破了多根均线，且当天伴有较大的成交量，即形成出水芙蓉形态。"
            ],
            
        }
        """
        chart_dict = {
            "10001": "chart_id=10004&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176244&sign=6939280f77e632c92af9187f2ce214d6",
            "10002": "chart_id=10002&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176246&sign=cf90a929ed5e055319189a3d6f4d31b0",
            "10003": "chart_id=10003&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176247&sign=3e1f5fedaa8224dcd7b337a2c66a3dce",
            "10004": "chart_id=10004&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176244&sign=6939280f77e632c92af9187f2ce214d6",
            
            "20013": "chart_id=20013&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176253&sign=20bfd6696b27aff4aeb60818f1060df8",
            "20014": "chart_id=20014&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617175792&sign=c6f245d7c41f6ebddd49d3219deae340",
            "20015": "chart_id=20015&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176252&sign=61ba8d940ac02c37ec204c7602632758",
            "20016": "chart_id=20016&offset=0&limit=30&exchange=12&user_type=4&source=xg&time=1617176256&sign=cf353d35952c7701b57820ddf08027fe",
            
        }

        url = "https://bisheng.tenpay.com/fcgi-bin/xg_cp_detail.fcgi?user_type=4&guid=740C80E4-622B-4263-AAFE-36481DC488B0&_appName=ios&_dev=iPhone12,5&_devId=4f4fa86d37f46aa2e5b759b9dbf719a8b557ec59&_appver=8.8.5&_ifChId=&_isChId=1&_osVer=14.2&openid=oA0GbjhnCVhlaOlVm5vXzQy-28Mo&fskey=v0aaf8a822160499a0b98c1a62919ba5&appid=wxcbc3ab3807acb685&access_token=43_nxkkOvNEbx5qg3wBnjGTrbInYbR3LPqm3ndZcZRXyns3o-rD5ccL_g-5RSNR5uqQKUsqzPIBVacoGDCl4Ix0col6R8fkc_hodKylQsgGs5E&buildType=store&check=11&_idfa=00000000-0000-0000-0000-000000000000&lang=zh_CN"
        h = {
            "user-agent": "QQStock/21022615 CFNetwork/1206 Darwin/20.1.0",
            "content-type": "application/x-www-form-urlencoded",
        }

        data = chart_dict.get(chart_id)
        r = requests.post(url, headers=h,data=data)
        rj = r.json()
        return rj


class SystemStrategy():
    @classmethod
    def get_client(cls, url ,data):
        headers = {
            "user-agent": "QQStock/21022615 CFNetwork/1206 Darwin/20.1.0",
            "content-type": "application/x-www-form-urlencoded",
        }
        r = requests.post(url, data=data, headers=headers)
        return r.json()['data']

    @classmethod
    def volue_rule(cls, sid=None):
        """
        {
            "3003": [
                "主力抢筹",
                "连续3个交易日主力资金净流入大于0，且主力资金净流入大于散户",
                "资金流向是引起股票涨跌的最直接原因。本策略依靠对盘面成交明细的大数据分析，持续扫描盘中主力资金大幅买入的个股，挖掘个股机会”
            ],
            "6010":{
                    "name": "国家队动向",
                    "logic": "国家队季度持仓环比增加",
                    "period": 61,
                    "sort_field": "change_percent",
                    "tags": "中线,事件型"
            },
            "3001": {
                "logic": "过去5个交易日北向资金净流入前5名",
                "name": "港资热捧",
                "period": 7,
                "sort_field": "change_percent",
                "tags": "短线,资金面"
            },
            "2011":{
                "logic": "前一交易日的20日乖离率小于0并创10日新低，且当日涨跌幅大于5%",
                "name": "超跌反弹",
                "period": 6,
                "sort_field": "change_percent",
                "tags": "短线,技术面",
                "description": "超跌反弹是常见的技术形态。该策略关注短期内经过大幅杀跌的个股。该类股票技术上存在修复需求，一旦出现长阳确认，后市存在一定市场机会",
            },
            "6012":{
                "logic": "最新季度QFII持仓环比增加",
                "name": "QFII增仓",
                "period": 61,
                "sort_field": "change_percent",
                "tags": "中线,事件型",
                "description": "比较季度之间重要股东持股变化，识别境外机构投资者（QFII）最新调仓买入的个股。"
            },
            "3005":{
                "logic": "连续3个交易日融资余额增加，且日涨跌幅大于0",
                "name": "融资追涨",
                "period": 61,
                "sort_field": "change_percent",
                "tags": "短线,资金面",
                "description": "融资是指投资者向证券公司借入资金买入股票的行为，融资越多对后市信心越大。本策略通过观察A股融资标的余额的变化，紧密跟踪杠杆资金追击的个股。"
            },

            "3006":{
                "logic": "龙虎榜上有机构买入的个股",
                "name": "机构接盘",
                "period": 1,
                "sort_field": "change_percent",
                "tags": "短线,资金面",
                "description": "专业机构的操作是市场关注的焦点。交易所在每日盘后会公布成交活跃的机构。股票龙虎榜单中出现“机构专用”买入，则说明有机构看好后市"
            },
            "6013":{
                "logic": "最新季度保险持仓环比增加",
                "name": "保险增仓",
                "period": 61,
                "sort_field": "change_percent",
                "tags": "中线,事件型",
                "description": "比较季度之间重要股东持股变化，识别保险资金最新调仓买入的个股。",
            },

            "2010":{
                "logic": "收盘价大于开盘价，且最低价大于前一交易日最高价的1.01倍",
                "name": "跳空向上",
                "period": 6,
                "sort_field": "change_percent",
                "tags": "短线,分享最多,技术面",
                "description": "相邻交易日的股价一般是连续变动的，两根K线间没有空缺区。行情强势状态时，会出现多方追涨或空方杀跌，缺口就会出现。如果股价跳空向上形成缺口，意味着短线买方力量较强。"
            },
            "6008": {
                "logic": "最新业绩净利润同比增速大于50%",
                "name": "业绩预增",
                "period": 61,
                "sort_field": "fdeclare_date",
                "tags": "短线,事件型",
                "description": "交易所规定，财报业绩大幅改善的公司须提前发布业绩公告，减少信息不对称。本策略第一时间挖掘财报业绩公告，提前布局，赚取基本面向好所带来的公司价值收益"
            },
            "2008": {
                "logic": "KDJ金叉，k值小于20，且日成交量大于上一个交易日的2倍",
                "name": "黄金KDJ",
                "period": 6,
                "sort_field": "change_percent",
                "tags": "短线,技术面",
                "description": "KDJ是经典的技术指标，标准用法为金叉买入，死叉卖出。但为避免追高，会追踪在低位的KDJ金叉，其股价企稳反弹迹象明显"
            },
            "6001": {
                "logic": "最新高管增持记录",
                "name": "高管增持",
                "period": 61,
                "sort_field": "fdeclare_date",
                "tags": "短线,事件型",
                "description": "上市公司高管的增持行为彰显其对公司的信心。股价下跌时，高管可能通过增持来稳定股价"
            },
            "2012": {
                "logic": "过去10个交易日出现过涨停，涨停后每日收阳线，且阳线数量大于等于4",
                "name": "长阳指路",
                "period": 6,
                "sort_field": "change_percent",
                "tags": "短线,技术面",
                "description": "该策略从涨停之后的K线形态入手，个股在拉出一个涨停板之后，再以斜坡碎步向上吸筹"
            },
            "2009": {
                "logic": "5日/10日/20日均线呈多头排列，且股价向上突破20日布林中轨",
                "name": "多头突破",
                "period": 6,
                "sort_field": "change_percent",
                "tags": "短线,技术面",
                "description": "均线常用于描述重要的支撑价位。而布林通道中轨代表股价震荡区间的中枢价格。一旦股价形成多头排列且在震荡指标上得到确认，那么相互验证的技术信号可以强化股价的后期走势。"            },
            "3004": {
                "logic": "尾盘15分钟低位拉升幅度大于4%，且成交量大于当日均值的2倍",
                "name": "尾盘掘金",
                "period": 1,
                "sort_field": "change_percent",
                "tags": "短线,资金面",
                "description": "收盘前的短时间内，主力资金面临的不确定性缩小，会利用资金优势，提前布局次日交易。通过扫描尾盘快速拉升且成交量放大的个股，为投资者提供独特的交易视角。"
            }
        }
        """

        if sid is None:
            sid = "3003"

        p__ = [
            "sid=2008&months=3&exchange=12&user_type=4&source=xg&time=1617193509&sign=6c7217e2ff08c562ecfd96f5769e50e6",
            "sid=2009&months=3&exchange=12&user_type=4&source=xg&time=1617193673&sign=07ad2523c43153df6d01723720c5b92e",
            "sid=2010&months=3&exchange=12&user_type=4&source=xg&time=1617193403&sign=53b85728e2347a2d2c1814e6957ccaa5",
            "sid=2012&months=3&exchange=12&user_type=4&source=xg&time=1617193616&sign=365dda081c85074d794f11c20df29a0a",

            "sid=3001&months=3&exchange=12&user_type=4&source=xg&time=1617192998&sign=90440c2bb966d8a2e38a344cbf4fff4f",
            "sid=3003&months=3&exchange=12&user_type=4&source=xg&time=1617178769&sign=8451011035835d97c1e4395933ebeecb",
            "sid=3005&months=3&exchange=12&user_type=4&source=xg&time=1617193233&sign=ca72dedd4df1c0410c12f794879bdd5d",
            "sid=3004&months=3&exchange=12&user_type=4&source=xg&time=1617194136&sign=a65c6df831c803a107dff65a21237e15",
            "sid=3006&months=3&exchange=12&user_type=4&source=xg&time=1617193296&sign=fbf2af432cea44927ff3a221c262a2a8",

            "sid=6001&months=3&exchange=12&user_type=4&source=xg&time=1617193552&sign=7d81f3b60290979b3ea7e7123d5036c5",
            "sid=6010&months=3&exchange=12&user_type=4&source=xg&time=1617192897&sign=f8301c9eb5e0fe43f7cde7e2431b7851",
            "sid=2011&months=3&exchange=12&user_type=4&source=xg&time=1617193080&sign=76d26ad71333a8517a528261cc1bb2e4",
            "sid=6012&months=3&exchange=12&user_type=4&source=xg&time=1617193147&sign=fb50958cb9863f8e4d69267aea121aef",
            "sid=6013&months=3&exchange=12&user_type=4&source=xg&time=1617193342&sign=fb40c5b83b51d36a9459f6be18382336",
            "sid=6008&months=3&exchange=12&user_type=4&source=xg&time=1617193451&sign=490881a15ea8e15221aa3801f7421450",
        ]
        info = dict()
        for x in p__:
            if not x:
                continue

            n = x.split("&")[0].split("=")[1]
            info[n] = x
        data = info[sid]
        url = "https://bisheng.tenpay.com/fcgi-bin/xg_qry_system_strategy_detail.fcgi?user_type=4&guid=2DD3F12F-F6CB-4F3A-BBA3-C6121530FA56&_appName=ios&_dev=iPhone12,5&_devId=4f4fa86d37f46aa2e5b759b9dbf719a8b557ec59&_appver=8.8.5&_ifChId=&_isChId=1&_osVer=14.2&openid=oA0GbjhnCVhlaOlVm5vXzQy-28Mo&fskey=v0aaf8a822160499a0b98c1a62919ba5&appid=wxcbc3ab3807acb685&access_token=43_nxkkOvNEbx5qg3wBnjGTrbInYbR3LPqm3ndZcZRXyns3o-rD5ccL_g-5RSNR5uqQKUsqzPIBVacoGDCl4Ix0col6R8fkc_hodKylQsgGs5E&buildType=store&check=11&_idfa=00000000-0000-0000-0000-000000000000&lang=zh_CN"
        # data = "sid=3003&months=3&exchange=12&user_type=4&source=xg&time=1617178769&sign=8451011035835d97c1e4395933ebeecb"
        rsp = cls.get_client(url, data)
        return pd.DataFrame(rsp['stocks'])
        